package com.zimmor.mq.rocket_train.base;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @Author:zimmor
 * @Date: 2023/2/10 15:59
 * @Description 普通发送
 * @version: 1.0.0
 */

public class SyncProducer {
    public static void main(String[] args) throws MQClientException {
        //创建producer
        DefaultMQProducer producer1 = new DefaultMQProducer("consumer-group1");
        //设置namesrv
        producer1.setNamesrvAddr("192.168.204.136:9876;192.168.204.137:9876");
        //启动producer
        producer1.start();
        //构建message
        for (int i = 0; i <10 ; i++) {
            Message message = new Message("base1", "tag1", ("sync-message" + i).getBytes());
            try {
                SendResult sendresult = producer1.send(message);
                System.out.println(sendresult.toString());
            } catch (MQClientException e) {
                e.printStackTrace();
            } catch (RemotingException e) {
                e.printStackTrace();
            } catch (MQBrokerException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        //发送同步消息
        producer1.shutdown();
    }
}
